package com.mcxx.modules.miniSalvation.fundpay.service.read;

import com.alibaba.fastjson.JSON;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.base.service.read.AreaHelper;
import com.mcxx.modules.miniSalvation.family.dto.SalvationFamilyDTO;
import com.mcxx.modules.miniSalvation.fundpay.dao.read.FundRecoverReadDao;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundRecoverDTO;
import com.mcxx.modules.miniSalvation.fundpay.dto.FundRecoverExcelDTO;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundRecoverEntity;
import com.mcxx.modules.miniSalvation.fundpay.service.write.FundRecoverWriteService;
import com.mcxx.modules.miniSalvation.salary.param.SalvationRecoveryParam;
import com.mcxx.util.CommonUtil;
import com.mcxx.util.Constant;
import com.mcxx.util.DateUtil;
import com.mcxx.util.FailureCode;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.ParseException;
import java.time.LocalDate;
import java.util.Calendar;
import java.util.List;

@Service
@Slf4j
@TaTransactional(readOnly = true)
public class FundRecoverReadService {

    @Autowired
    private FundRecoverReadDao fundRecoverReadDao;
    @Autowired
    private FundRecoverWriteService fundRecoverWriteService;
    @Autowired
    private AreaHelper areaHelper;




    public FundRecoverEntity getById(String id) {
    	return fundRecoverReadDao.getById(id);
    }


    public Page<SalvationFamilyDTO> getSalvationRecoveryFamilyList(SalvationRecoveryParam param, PageParam pageParam) {
        fundRecoverReadDao.beginPager(pageParam);
        List<SalvationFamilyDTO>list =fundRecoverReadDao.getSalvationRecoveryFamilyList(param);
        return fundRecoverReadDao.endPager(list);
    }


    public Page<FundRecoverDTO> getRecoveryList(SalvationRecoveryParam param, PageParam pageParam) {
        fundRecoverReadDao.beginPager(pageParam);
        List<FundRecoverDTO>list =fundRecoverReadDao.getRecoveryList(param);
        return fundRecoverReadDao.endPager(list);
    }

    public List<FundRecoverExcelDTO> getRecoveryListExcel(SalvationRecoveryParam param){
        return fundRecoverReadDao.getRecoveryListExcel(param);
    }


    public void verifyRecover(FundRecoverEntity param){

        FundRecoverEntity fundRecoverEntity=  fundRecoverReadDao.queryByMemberID(param);
        if(!CommonUtil.invalidArgs(fundRecoverEntity)){
            throw new BizException("-1",FailureCode.ERR_10002.getMessage());
        }
    }


    public void verifyMoney(FundRecoverEntity param){

        SalvationFamilyDTO salvationFamilyDTO=  fundRecoverReadDao.querysalaryNumberByMemberID(param);
        if(CommonUtil.invalidArgs(salvationFamilyDTO)){
            throw new BizException("-1","违规年月不是在待遇时间内，请重新选择");
        }

        int salaryCount = DateUtil.calDiffMonth(param.getStartDate(),param.getEndDate())+1;
        if((Double.parseDouble(salvationFamilyDTO.getSalaryNumber())*salaryCount)<param.getRecoverMoney()){
            throw new BizException("-1","追回金额大于保障金额");
        }
    }


    public int addSalaryRecovery(FundRecoverEntity param) {

        FundRecoverEntity fundRecoverEntity=  fundRecoverReadDao.queryByMemberID(param);
        if(!CommonUtil.invalidArgs(fundRecoverEntity)){
            throw new BizException(FailureCode.ERR_10002.getMessage());
        }
        param.setState(Constant.RecoveryState.UNCONFIRM);
        param.setId(Constant.getUUID());
        param.setCreateTime(Date.valueOf(LocalDate.now()));
        log.info("登记追回救助金"+ JSON.toJSONString(param));
        return fundRecoverWriteService.insertIgnoreNull(param);
    }

    public FundRecoverDTO selectRecoverInfo(String id){
        return fundRecoverReadDao.selectRecoverInfo(id);
    }

}
